package group.igfd.manager.biz.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileUtil {

    private static Logger log = LoggerFactory.getLogger(FileUtil.class);

    /**
     * NIO way
     */
    public static byte[] toByteArray(String filename) {

        File f = new File(filename);
        if (!f.exists()) {
            log.error("文件未找到！" + filename);
            throw new RuntimeException("file not found");
        }
        FileChannel channel = null;
        FileInputStream fs = null;
        try {
            fs = new FileInputStream(f);
            channel = fs.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
            while ((channel.read(byteBuffer)) > 0) {
                // do nothing
                // System.out.println("reading");
            }
            return byteBuffer.array();
        } catch (IOException e) {
            throw new RuntimeException("file read error");
        } finally {
            try {
                channel.close();
            } catch (IOException e) {
                throw new RuntimeException("file read error");
            }
            try {
                fs.close();
            } catch (IOException e) {
                throw new RuntimeException("file read error");
            }
        }
    }

    /**
     * 删除目录
     *
     * @author fengshuonan
     * @Date 2017/10/30 下午4:15
     */
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }


    /**
     * Stream To File
     * @param ins
     * @param file
     */
    public static String inputStream2ToFile(InputStream ins, File file) {

        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return file.getPath();
    }

    /**
     * 根据byte数组，生成临时文件
     * @param bfile
     * @return
     */
    public static File byte2File(byte[] bfile){
        BufferedOutputStream bos=null;
        FileOutputStream fos=null;
        File imgFile=null;
        try{
            imgFile = File.createTempFile(IdGenerator.getId(), ".jpg");
            fos=new FileOutputStream(imgFile);
            bos=new BufferedOutputStream(fos);
            bos.write(bfile);
        }
        catch(Exception e){
//            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        finally{
            try{

                if(bos != null){
                    bos.close();
                }
                if(fos != null){
                    fos.close();
                }
            }
            catch(Exception e){
//                System.out.println(e.getMessage());
                e.printStackTrace();
            }
        }
        return imgFile;
    }


}